package com.guanglan.shrio;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.guanglan.domain.SysPermission;
import com.guanglan.domain.SysRole;
import com.guanglan.domain.SysUser;
import com.guanglan.service.SysPermissionService;
import com.guanglan.service.SysRolePermissionService;
import com.guanglan.service.SysRoleService;
import com.guanglan.service.SysUserRoleService;
import com.guanglan.service.SysUserService;

@Component
public class WebShiroComponent {
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRolePermissionService sysRolePermissionService;
	@Autowired
	private SysPermissionService sysPermissionService;
	
	/**
	 * 通过用户名查找其所有角色
	 * @param userName
	 * @return
	 */
	public List<SysRole> getRoles(String userName){
		SysUser sysUser = sysUserService.selectByUserName(userName);
		List<Long> roleIds = sysUserRoleService.selectByUserId(sysUser.getId());
		List<SysRole> sysRoles = sysRoleService.selectByRoleId(roleIds);
		sysRoles.forEach(r -> {
			System.out.println("<=======>"+r.getRoleName());
		});
		return sysRoles;
	}
	
	/**
	 * 通过用户名查找其所有菜单权限
	 * @param userName
	 * @return
	 */
	public List<SysPermission> getPermissions(String userName){
		SysUser sysUser = sysUserService.selectByUserName(userName);
		List<Long> roleIds = sysUserRoleService.selectByUserId(sysUser.getId());
		List<SysRole> sysRoles = sysRoleService.selectByRoleId(roleIds);
		List<Long> permissionIds = sysRolePermissionService.selectPermissionIdByRoleId(sysRoles);
		List<SysPermission> sysPermissions = sysPermissionService.selectByPermissionId(permissionIds);
		return sysPermissions;
	}
	

}
